package com.xcity.game.combat;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.unit.CombatUnit;

import naga.x.util.CommonUtils;

/**
 * 一个战斗队伍
 * @author yang.li
 *
 */
public class CombatTeam {
	
	protected List<CombatUnit> members; // 战队成员(leader在第一位)
	
	public static final int DEFAULT_TEAM_CAPACITY = 5;
	
	public CombatTeam() {
		this(DEFAULT_TEAM_CAPACITY);
	}
	
	public CombatTeam(int capacity) {
		members = new ArrayList<CombatUnit>(capacity);
	}
	
	public void init() {
		for (int i = 0; i < members.size(); i++) {
			AbstractCombatUnit u = (AbstractCombatUnit) members.get(i);
			u.index = i;
		}
	}
	
	public void add(CombatUnit unit) {
		members.add(unit);
	}
	
//	public void set(int index, CombatUnit unit) {
//		members.set(index, unit);
//	}
	
	public void remove(CombatUnit unit) {
		members.remove(unit);
	}
	
	public int size() {
		return members.size();
	}
	
	public List<CombatUnit> all() {
		return members;
	}
	
	public List<CombatUnit> getMembers() {
		return members.subList(1, members.size());
	}
	
	public CombatUnit getLeader() {
		return members.get(0);
	}
	
	public CombatUnit find(long unitId) {
		for (int i = size() - 1; i >= 0; i--) {
			CombatUnit unit = members.get(i);
			if (unit.getId() == unitId) {
				return unit;
			}
		}
		return null;
	}
	
	/**
	 * 获取当前防守单位
	 * @return
	 */
	public CombatUnit getDefenseUnit(int index) {
		// 优先向前查找
		for (int i = index; i >= 0; i--) {
			CombatUnit unit = members.get(i);
			if (!unit.isDied()) {
				return unit;
			}
		}
		// 未找到再向后查找
		for (int i = index; i < size(); i++) {
			CombatUnit unit = members.get(i);
			if (!unit.isDied()) {
				return unit;
			}
		}
		return null;
	}
	
	@JSONField(serialize = false)
	public CombatUnit getUnitRandomly() {
		List<CombatUnit> units = new ArrayList<CombatUnit>(size());
		for (int i = 0; i < size(); i++) {
			CombatUnit unit = members.get(i);
			if (!unit.isDied()) {
				units.add(unit);
			}
		}
		if (units.isEmpty()) {
			return null;
		}
		int index = Combat.RND.nextInt(units.size());
		return units.get(index);
	}
	
	public List<CombatUnit> getUnitsRandomly(int size) {
		List<CombatUnit> units = new ArrayList<CombatUnit>(size());
		for (int i = 0; i < size(); i++) {
			CombatUnit unit = members.get(i);
			if (!unit.isDied()) {
				units.add(unit);
			}
		}
		if (units.isEmpty()) {
			return null;
		}
		if (units.size() <= size) {
			return units;
		}
		CommonUtils.shuffle(units, Combat.RND);
		return units.subList(0, size);
	}
	
//	private void moveTurn() {
//		if (++turn >= size()) {
//			turn = 0;
//		}
//	}

	@JSONField(serialize = false)
	public boolean isDied() {
		for (int i = size() - 1; i >= 0; i--) {
			CombatUnit unit = members.get(i);
			if (!unit.isDied()) {
				return false;
			}
		}
		return true;
	}
	
	public int sizeOfAlive() {
		return size() - sizeOfDead();
	}
	
	public int sizeOfDead() {
		int c = 0;
		for (int i = size() - 1; i >= 0; i--) {
			CombatUnit unit = members.get(i);
			if (unit.isDied()) {
				++c;
			}
		}
		return c;
	}
	
//	public void send(Packet packet) {
////		for (int i = 0; i < size(); i++) {
////			CombatUnit unit = members.get(i);
////			unit.send(packet);
////		}
//		getLeader().send(packet);
//	}
	
}
